package sw6.girafplaceclient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import girafPlace.lib.*;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AppView extends LinearLayout {

	protected Context mContext;
    protected ImageView image;
    protected TextView nameTextView;
    protected Application app;
    protected String tag = "GIRAFPLACE";
	
	public AppView(Context context, Application app) {
		super(context);
		
		mContext = context;
		this.app = app;
		this.setOrientation(LinearLayout.HORIZONTAL);
		
		image = new ImageView(mContext);
        nameTextView = new TextView(mContext);
        
        image.setMaxHeight(100);
        image.setMinimumHeight(100);
        image.setMaxWidth(100);
        image.setMinimumWidth(100);
        
        nameTextView.setTextSize(20);
        setGravity(Gravity.CENTER_VERTICAL);
        
        this.addView(image);
        this.addView(nameTextView);
        
        new DownloadIcon().execute();
	}
	
	public void setApp(Application app) {
		this.app = app;
		nameTextView.setText(app.getName());
		new DownloadIcon().execute();
	}
	
	public Application getApp() {
		return this.app;
	}
	
	private class DownloadIcon extends AsyncTask<Void, Void, String> {

		@Override
		protected void onPreExecute() {
			Log.d(tag, "Preparing to start the download icon task");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... voids) {
			try {
				Log.d(tag,"Starting the download task, first by building the string");
				String urlString = "http://girafplace.lcdev.dk/icos/";
				urlString = urlString + Integer.toString(app.getId()) + ".png"; 

				Log.d(tag,String.format("The string is: %1$s",urlString));

				URL url = new URL(urlString);

				File file = new File(mContext.getCacheDir(), app.getName() + ".png");

				Log.d(tag,"Opening connection");
				URLConnection ucon = url.openConnection();

				ucon.connect();
				

				BufferedInputStream bis = new BufferedInputStream(ucon.getInputStream());

				ByteArrayBuffer baf = new ByteArrayBuffer(200);

				Log.d(tag,"Starting to read");
				int current = 0;
				while((current = bis.read()) != -1 && !isCancelled()) {
					baf.append((byte) current);
				}

				FileOutputStream fos = new FileOutputStream(file);
				fos.write(baf.toByteArray());
				fos.close();

				if(isCancelled())
					return null;
				else
					return file.getPath();
			} catch (Exception e) {
				Log.e(tag,e.getMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {	
			if(result != null) {
				image.setImageDrawable(new BitmapDrawable(result));
			}
		}
	}
}